package mysale.web;

import mysale.pojo.Cart;
import mysale.pojo.Food;
import mysale.pojo.User;
import mysale.service.impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author SnakeWoodMan
 * @create 2020-12-29 17:28
 */
public class CartServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    static String selectedCfid = null;
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String operation = request.getParameter("op");
        CartServiceImpl cartService = new CartServiceImpl();
        /**查询所有订单*/
        if (operation.equals("search")) {
            //判断是否是登录用户，是，跳转到购物车界面；不是，跳转到登录界面
            //从session会话中获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            //获取用户的uid并存放在变量uid中
            if (user != null) {
                String uid = String.valueOf(user.getUid());
                List<Cart> cartList = cartService.getallinfo(uid);
                //1.获取要播放的歌曲对象
                request.getSession().setAttribute("cartList", cartList);
                request.getRequestDispatcher("/cartList.jsp").forward(request, response);

            } else {
                //重定向到登录界面
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
            /**通过关键词搜索零食*/
        }else if(operation.equals("searchbuy")){
            User user = (User) request.getSession().getAttribute("user");
            //获取用户的uid并存放在变量uid中
            if (user != null) {
                String uid = String.valueOf(user.getUid());
                List<Cart> cartList = cartService.getbuyinfo(uid);
                //1.获取要播放的歌曲对象
                request.getSession().setAttribute("cartList", cartList);
                request.getRequestDispatcher("/cartList.jsp").forward(request, response);

            } else {
                //重定向到登录界面
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
            /**查询购物车里所有没有购买的零食*/
        }else if(operation.equals("searchnotbuy")){
            User user = (User) request.getSession().getAttribute("user");
            //获取用户的uid并存放在变量uid中
            if (user != null) {
                String uid = String.valueOf(user.getUid());
                List<Cart> cartList = cartService.getnotbuyinfo(uid);
                //1.获取要播放的歌曲对象
                request.getSession().setAttribute("cartList", cartList);
                request.getRequestDispatcher("/cartList.jsp").forward(request, response);

            } else {
                //重定向到登录界面
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
            /**添加物品到购物车**/
        } else if (operation.equals("addGoodsToCart")) {
            //从session会话中获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            if (user != null) {
                //获取商品的购买数量
                String number = request.getParameter("number");
                //从user中获取当前用户的uid
                String uid = String.valueOf(user.getUid());
                //获取商品fid
//                ********************
                Food food = null;
                request.getSession().setAttribute("food", food);
                String nowfid = FoodServlet.selectedFid;
//                System.out.println("nowfid:" + nowfid);
//                System.out.println("/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/");
                String[] sourceStrArray = nowfid.split("=");
                String fid = sourceStrArray[2];
//                System.out.println("/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/");
//                System.out.println("fid" + fid);
                Cart cart = new Cart();
                cart.setNumber(number);
                cart.setCuid(uid);
                cart.setCfid(fid);
                List <Cart> numlist = cartService.getcartbyuiffid(cart);
                if (numlist.size()==1){//如果已经存在数据就退出
                    return;
                }
                boolean result = cartService.addGoodsToCart(cart);
                if (result) {
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('加入购物车成功!');</script>");
                    response.sendRedirect(request.getContextPath() + "/foodList.jsp");
                } else {
//                    ***************先做show才能做添加购物车
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('加入购物车失败!');</script>");
                    response.sendRedirect(request.getContextPath() + "/foodList.jsp");
//                    request.setAttribute("msg","账号已存在，请重新输入！");
//                    request.getRequestDispatcher("/regist.jsp").forward(request, response);
                }
            } else {
                //重定向到登录界面
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
            /**从购物车中删除零食*/
        }else if (operation.equals("jixuaddGoodsToCart")) {
            //从session会话中获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            if (user != null) {
                //获取商品的购买数量
                String number = request.getParameter("number");
                //从user中获取当前用户的uid
                String uid = String.valueOf(user.getUid());
                //获取商品fid
//                ********************
                Food food = null;
                request.getSession().setAttribute("food", food);
                String nowfid = selectedCfid;
                String[] sourceStrArray = nowfid.split("=");
                String fid = sourceStrArray[2];
                String[] strs = fid.split("&");

//                System.out.println("///////*/**********///nowfid:" + strs[0]);
//                System.out.println("/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/");

//                System.out.println("/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/");
//                System.out.println("fid" + fid);
                Cart cart = new Cart();
                cart.setNumber(number);
                cart.setCuid(uid);
                cart.setCfid(strs[0]);
                List <Cart> numlist = cartService.getcartbyuiffid(cart);
                if (numlist.size()==1){//如果已经存在数据就退出
                    return;
                }
                boolean result = cartService.addGoodsToCart(cart);
                if (result) {
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('加入购物车成功!');</script>");
                    response.sendRedirect(request.getContextPath() + "/foodList.jsp");
                } else {
//                    ***************先做show才能做添加购物车
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('加入购物车失败!');</script>");
                    response.sendRedirect(request.getContextPath() + "/foodList.jsp");
//                    request.setAttribute("msg","账号已存在，请重新输入！");
//                    request.getRequestDispatcher("/regist.jsp").forward(request, response);
                }
            } else {
                //重定向到登录界面
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
            /**从购物车中删除零食*/
        }
        else if (operation.equals("delete")) {
            //从session会话中获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            if (user != null) {
                //获取商品的购买数量
                //从user中获取当前用户的uid
                String uid = String.valueOf(user.getUid());
                //获取商品fid
//                ********************
                String cfid = request.getParameter("fid");
                System.out.println("cfid:"+cfid);
                Cart cart = null;
                List<Cart> list = (List<Cart>) request.getSession().getAttribute("cartList");
                System.out.println("list:"+list.size());
                for (Cart m: list ){
                    String strfid = m.getCfid();
                    System.out.println("strfid:"+strfid);
                    if (cfid.equals(strfid)){
                        cart = m;
                        break;
                    }
                }
                selectedCfid = request.getQueryString();
                System.out.println("selected fid: "+selectedCfid);
                boolean result = cartService.deleteGoodsFromCart(cart);
                if (result) {
                    response.setContentType("text/html;charset=utf-8");

                    response.sendRedirect(request.getContextPath() + "/cartList.jsp");
                    response.getWriter().write("<script>alert('删除成功!');</script>");
                } else {
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('删除失败!');</script>");
                }
            }
            /**购买购物车中的零食*/
        }else if (operation.equals("buy")){
            User user = (User) request.getSession().getAttribute("user");
            if (user != null) {
                //获取商品的购买数量
                //从user中获取当前用户的uid
                String uid = String.valueOf(user.getUid());
                //获取商品fid
                String cfid = request.getParameter("fid");
                Cart cart = null;
                List<Cart> list = (List<Cart>) request.getSession().getAttribute("cartList");
                for (Cart m: list ){
                    String strfid = m.getCfid();
                    System.out.println("strfid:"+strfid);
                    if (cfid.equals(strfid)){
                        cart = m;
                        break;
                    }
                }
                selectedCfid = request.getQueryString();
                //1.将购物车内物品标为已购买
                //2.减少库存数量
                //1和2需同时达成
                boolean result = cartService.buyGoodsFromCart(cart)
                        && cartService.subGoodsStock(cart);
                if (result) {
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('购买成功!');</script>");
                    response.sendRedirect(request.getContextPath() + "/cartList.jsp");
                } else {
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('购买失败!库存数量不足！');</script>");
                }
            }
        }else if (operation.equals("look")){
            User user = (User) request.getSession().getAttribute("user");
            if (user != null) {
                //获取商品的购买数量
                //从user中获取当前用户的uid
                String uid = String.valueOf(user.getUid());
                //获取商品fid
                String cfid = request.getParameter("fid");
                Cart cart = null;
                List<Cart> list = (List<Cart>) request.getSession().getAttribute("cartList");
                for (Cart m: list ){
                    String strfid = m.getCfid();
                    System.out.println("strfid:"+strfid);
                    if (cfid.equals(strfid)){
                        cart = m;
                        break;
                    }
                }
                selectedCfid = request.getQueryString();
                System.out.println("look selected fid: "+selectedCfid);
                request.getSession().setAttribute("cart",cart);
//                System.out.println(cart.getCpic());
                request.getRequestDispatcher("/lookfood.jsp").forward(request, response);
            }
        }else if(operation.equals("alertGoodsNum")){
            //从session会话中获取用户信息
            User user = (User) request.getSession().getAttribute("user");
            if (user != null) {
                //获取商品的购买数量
                String number = request.getParameter("number");
                //从user中获取当前用户的uid
                String uid = String.valueOf(user.getUid());
                //获取商品fid
                Food food = null;
                request.getSession().setAttribute("food", food);
                String nowfid = selectedCfid;
                System.out.println("cessss:"+selectedCfid);
                String[] sourceStrArray = nowfid.split("=");
                String fid = sourceStrArray[2];
                String[] strs = fid.split("&");

                System.out.println("strs[0]"+strs[0]);
                System.out.println("true_fid:"+selectedCfid);
                Cart cart = new Cart();
                cart.setNumber(number);
                cart.setCuid(uid);
                cart.setCfid(strs[0]);
                boolean result = cartService.updateGoodsNum(cart);
                if (result) {
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('修改数量成功!');</script>");
                    response.sendRedirect(request.getContextPath() + "/cartList.jsp");
                } else {
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("<script>alert('修改数量失败!');</script>");

                }
            } else {
                //重定向到登录界面
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
        }
    }
}